<?php

namespace App\Http\Middleware;

use App\Services\Cache\Key;
use Closure;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Distribution
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $agentID = (int)$request->input('agentid', 0); //分销人ID
        $openid = (string)$request->input('openid', '');
        if (empty($openid)) {
            Log::error('distribution middle缺少openid');
        }
        if ($openid && $agentID) {
            Log::info(sprintf('监测到分销 openid: %s agentId: %d', $openid, $agentID));
            Redis::connection()->setex(Key::getDistribution($openid), 86400, $agentID);
        }
        return $next($request);
    }
}
